package cn.powerone.scm.controller;

import java.util.List;

import cn.powerone.scm.service.IScmSequenceParmService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmDefectTicket;
import cn.powerone.scm.service.IScmDefectTicketService;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;

/**
 * 售后工单Controller
 * 
 * @author PowerOne
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/scm/scmDefectTicket")
public class ScmDefectTicketController extends BaseController
{
    @Autowired
    private IScmDefectTicketService scmDefectTicketService;
    @Autowired
    private IScmSequenceParmService scmSequenceParmService;

    /**
     * 查询售后工单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmDefectTicket:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmDefectTicket scmDefectTicket)
    {
        startPage();
        List<ScmDefectTicket> list = scmDefectTicketService.selectScmDefectTicketList(scmDefectTicket);
        return getDataTable(list);
    }

    /**
     * 导出售后工单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmDefectTicket:export')")
    @Log(title = "售后工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmDefectTicket scmDefectTicket)
    {
        List<ScmDefectTicket> list = scmDefectTicketService.selectScmDefectTicketList(scmDefectTicket);
        ExcelUtil<ScmDefectTicket> util = new ExcelUtil<ScmDefectTicket>(ScmDefectTicket.class);
        util.exportExcel(response, list, "售后工单数据");
    }

    /**
     * 获取售后工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:scmDefectTicket:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmDefectTicketService.selectScmDefectTicketById(id));
    }

    /**
     * 新增售后工单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmDefectTicket:add')")
    @Log(title = "售后工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmDefectTicket scmDefectTicket)
    {
        scmDefectTicket.setTicketCode(scmSequenceParmService.generateScmSequence("ticket_code"));
        return toAjax(scmDefectTicketService.insertScmDefectTicket(scmDefectTicket));
    }

    /**
     * 修改售后工单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmDefectTicket:edit')")
    @Log(title = "售后工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmDefectTicket scmDefectTicket)
    {
        return toAjax(scmDefectTicketService.updateScmDefectTicket(scmDefectTicket));
    }

    /**
     * 删除售后工单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmDefectTicket:remove')")
    @Log(title = "售后工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmDefectTicketService.deleteScmDefectTicketByIds(ids));
    }
}
